﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Buffalo.SMECommon.DAL.Models;
using System.Collections;
using Ext.Net;
using System.Data.Linq;
using Buffalo.Lib.Common;
using System.Linq.Expressions;

namespace Buffalo.SMECommon.DAL
{
    public class BaseDAL
    {
        public ModelDataContext db = new ModelDataContext();
        public bool SaveEntity<T>(T entity) where T : class
        {
            return SaveEntity<T>(entity, null);
        }
        public bool SaveEntity<T>(T entity, string[] uniqueFields) where T : class
        {
            try
            {
                EntityHelper entityHelper = new EntityHelper();

                if (entity == null)
                    return false;

                string IDEntity = Util.ToSafeString ( Util.GetValue(entity, "ID"));

                if (string.IsNullOrEmpty ( IDEntity ) || IDEntity.Equals("0"))
                {
                    Util.SetValue (entity, "ID", General.GenerateID());
                    return entityHelper.InsertEntity<T>(db, entity, uniqueFields);
                }
                else
                    return entityHelper.UpdateEntity<T>(db, entity, uniqueFields);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public bool SaveEntities<T>(string jsonData, string[] uniqueFields) where T : class
        {
            try
            {
                EntityHelper entityHelper = new EntityHelper();
                return entityHelper.SaveEntities<T>(db, new ModelDataContext(), jsonData, uniqueFields);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public List<T> GetListEntity<T>(string tableName, int limit, int start, string dir, string sort, string criteria, string searchText, out int total) where T : class
        {
            try
            {
                EntityHelper entityHelper = new EntityHelper();
                return entityHelper.GetListEntity<T>(db, tableName, string.Empty, limit, start, dir, sort, criteria, searchText, out total);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<T> GetListEntity<T>(string tableName, string whereClause, int limit, int start, string dir, string sort, string criteria, string searchText, out int total) where T : class
        {
            try
            {
                EntityHelper entityHelper = new EntityHelper();
                return entityHelper.GetListEntity<T>(db, tableName, whereClause, limit, start, dir, sort, criteria, searchText, out total);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public T GetEntity<T>(string IDEntity) where T : class
        {
            try
            {
                EntityHelper entityHelper = new EntityHelper();
                return entityHelper.GetEntity<T>(db, IDEntity);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public T GetEntityByOneField<T>(string fieldName, string value) where T : class
        {
            try
            {
                EntityHelper entityHelper = new EntityHelper();
                List<T> list = entityHelper.GetEntityListByOneField<T>(db, fieldName, value);
                return list != null && list.Count > 0 ? list[0] : default(T);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
